import urllib.request
import urllib.parse


# # 爬虫的编码
# s = '薛之谦'
# # 会将汉语进行编码
# a = urllib.parse.quote(s)
# print(a)


# 浏览器会自动编解码 pycharm 不会自动编解码

url = 'https://www.baidu.com/s?wd='
key_word = '薛之谦'
code_key_word = urllib.parse.quote(key_word)
url = url + code_key_word
print(url)
headers = {

    'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
    'Cookie': 'BIDUPSID=050884BF79605B3FE5B441130814BD2B; PSTM=1600135523; BAIDUID=050884BF79605B3FB4E3B4F336CDFE50:FG=1; BD_UPN=12314753; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; sug=3; sugstore=1; ORIGIN=0; bdime=0; BDUSS=0J0QnVyV2lMMFNEMEtmRWpRLWdTWlZ4ZzI5Wkt0YXpKZERVNUhWcEE2MGJGb2hmRVFBQUFBJCQAAAAAAAAAAAEAAAAPfeKmsK7C~cXctcTO2rnqMwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABuJYF8biWBfT; BDUSS_BFESS=0J0QnVyV2lMMFNEMEtmRWpRLWdTWlZ4ZzI5Wkt0YXpKZERVNUhWcEE2MGJGb2hmRVFBQUFBJCQAAAAAAAAAAAEAAAAPfeKmsK7C~cXctcTO2rnqMwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABuJYF8biWBfT; BD_HOME=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BD_CK_SAM=1; PSINO=3; COOKIE_SESSION=8131_0_8_0_15_7_1_0_5_4_2_3_0_0_1_0_1600250890_0_1600259020%7C9%230_0_1600259020%7C1; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; BDRCVFR[CLK3Lyfkr9D]=mk3SLVN4HKm; H_PS_645EC=2095Q1TdarCNVRr9ilYOnHk51HYr7VOda%2B%2B5r2jTHRuMltuw3coHrV%2FI%2Fyg; H_PS_PSSID=7540_32617_1439_31253_7609_7552_7608_32116_32718_22160; BDSVRTM=0'
}

request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
